package com.company.utils;

import com.company.entity.Contacts;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 常用联系人
 */
public class FrequentContacts {

    public static void write() throws IOException {
        FileOutputStream fos = new FileOutputStream("data.object");
        List<Contacts> contactsList = new ArrayList<>();
        contactsList.add(new Contacts("丁丁","13810222222","北京市海淀区天成路207号"));
        contactsList.add(new Contacts("王涛","13810000000","北京市东城区两广路300号"));
        contactsList.add(new Contacts("王星","17723819999","郑州市中原区陇海西路7号"));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(contactsList);
        oos.close();
    }
    @Test
    public static void read() throws IOException, ClassNotFoundException {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.object"));
            List<Contacts> contacts = (List<Contacts>)ois.readObject();
        for (Contacts contact : contacts) {
            System.out.println(contact);
        }
        ois.close();
    }

   /* public static void main(String[] args) {
        try {
        write();
        System.out.println("序列化成功");
            *//*read();
            System.out.println("反序列化成功");*//*
        } catch (IOException e) {
            e.printStackTrace();
        } *//*catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*//*
    }*/
    /*public static void writeObj() throws IOException, FileNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.object"));
        List<Contacts> contactsList = new ArrayList<>();
        contactsList.add(new Contacts("丁丁","13810222222","北京市海淀区天成路207号/13810222222/丁丁"));
        contactsList.add(new Contacts("王涛","13810000000","北京市东城区两广路300号/13810000000/王涛"));
        contactsList.add(new Contacts("王星","17723819999","郑州市中原区陇海西路7号/17723819999/王星"));
        oos.writeObject(contactsList);
        oos.close();
    }
    public static void readObj() throws IOException, FileNotFoundException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.object"));
    *//*Student stu = (Student) ois.readObject();
    System.out.println("姓名: "+stu.getName());
    System.out.println("性别: "+stu.getSex());
    System.out.println("年龄: "+stu.getAge());*//*
        List<Contacts> list = (List<Contacts>)ois.readObject();
        for (Contacts contacts : list) {
            System.out.println(contacts);
        }
    }*/
}
